

#include <createDrawingButton.h>
#include <renderer.h>
#include <input.h>

CreateDrawingButton::CreateDrawingButton(){}
CreateDrawingButton::~CreateDrawingButton(){}


void CreateDrawingButton::setup(){
Button::setup();

}

void CreateDrawingButton::update(double deltaTime){

if (input->focusButton==this)
  {
  int mouseX=input->mouseX;
  int mouseY=input->mouseY;

  renderer->pick(mouseX,mouseY);

  if (newDrawing && input->worldTarget && input->worldTarget->bPickable)
    newDrawing->setLocation(input->mouse3D);
  }
}

void CreateDrawingButton::mouseOver(){

Button::mouseOver();
}

void CreateDrawingButton::mouseDrag(){}
void CreateDrawingButton::clickedLeft(){

cout << buttonProperty << endl;

std::string stringName=buttonProperty;
stringName.append(name);
TiXmlDocument doc( stringName );

if (!doc.LoadFile())
  {
  cout << "there was an error!" << endl;
  return;
  }

cout << "Loading..." <<endl;

TiXmlHandle hDoc(&doc);
TiXmlElement * myInfo;

myInfo=hDoc.FirstChildElement().Element();
// should always have a valid root but handle gracefully if it doesn't
if (!myInfo) return;

TiXmlHandle hRoot(0);
// save this for later
hRoot=TiXmlHandle(myInfo);

//***********************************************************************
//Load Drawing
//***********************************************************************


  TiXmlElement* element=hRoot.FirstChild( "Actor" ).Element();
  string myType;
  for( ; element!=NULL; element=element->NextSiblingElement())
	{
    cout << element->Value() << " " << element->GetText() <<endl;
	myType=element->GetText();
	Actor * A=renderer->actorInfo[myType].actorReference;
	A->create();
    A=renderer->actorList.back();
    //***********************************************************************
    //Fill up Properties
    //***********************************************************************
    A->load(element);
    A->setup();
    newDrawing=A;
    newDrawing->bPickable=false;
    }
input->focusButton=this;
cout << newDrawing->name << "... is my name" << endl;
}

void CreateDrawingButton::clickedRight(){}
void CreateDrawingButton::focusClick(){

Button::focusClick();
newDrawing->bPickable=true;
newDrawing=NULL;
}

void CreateDrawingButton::deselect(int depth){

Button::deselect(depth);
}

void CreateDrawingButton::create(){renderer->addButton(this);}
